package com.multi.job;

import com.multi.common.core.domain.entity.BkTenantInfo;
import com.multi.common.core.domain.entity.SysUser;
import com.multi.system.mapper.SysUserMapper;
import com.multi.tenant.domain.BkTenantSignupInfo;
import com.multi.tenant.mapper.BkTenantInfoMapper;
import com.multi.tenant.service.IBkTenantSignupInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * @Description
 * @ClassName JobTask
 * @Author pzj
 * @Data 2024/11/18 15:06
 */
@Slf4j
@Component
public class TenantJobTask {

    @Autowired
    private BkTenantInfoMapper bkTenantInfoMapper;

    @Autowired
    private IBkTenantSignupInfoService bkTenantSigupInfoService;

    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 检测租户是否快到期，如果7天内到期，则发送短信提示
     */
    //@Scheduled(cron = "0 0 12 * * ?")//每天12点执行
    public void checkTenant() {
        List<BkTenantSignupInfo> bkTenantSigupInfos = bkTenantSigupInfoService.selectCheckTenant();
        for (BkTenantSignupInfo bkTenantSigupInfo : bkTenantSigupInfos) {
            //TODO 这里可以推送微信模板信息，也可以推送短信
        }
    }

    /**
     * 停用服务
     */
    //@Scheduled(cron = "0 0 23 * * ?")//每天晚上23点执行
    public void stopService() {
        List<BkTenantSignupInfo> bkTenantSigupInfos = bkTenantSigupInfoService.selectStopService();
        //将到期租户停用、租户用户停用
        for (BkTenantSignupInfo bkTenantSigupInfo : bkTenantSigupInfos) {
            CompletableFuture.runAsync(() -> {
                Long tenantId = bkTenantSigupInfo.getTenantId();
                BkTenantInfo bkTenantInfo = bkTenantInfoMapper.selectBkTenantInfoById(tenantId);
                //将状态设置为停用
                bkTenantInfo.setStatus(1);
                bkTenantInfoMapper.updateBkTenantInfo(bkTenantInfo);
                //将租户用户停用
                List<SysUser> userList = sysUserMapper.selectByTenantId(tenantId);
                for (SysUser sysUser : userList) {
                    sysUser.setStatus("1");
                    sysUserMapper.updateUser(sysUser);
                }
            });
        }
    }

}
